package com.xauto.pioneer.appuser.view;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.mine.SettingActivity;
import com.xauto.pioneer.appuser.model.ShareInfo;
import com.xauto.pioneer.appuser.util.StringUtils;

/**
 * 分享自定义对话框
 */
public class ShareDialog extends Dialog {

    private Context context;
    private Activity activity;
    private ShareAction shareAction;

    private UMImage image; //分享图片
    private UMImage thumb; //缩略图片

    public ShareDialog(Context context, Activity activity, ShareInfo share) {
        super(context, R.style.LoadingDialogTheme);
        this.activity = activity;
        shareAction = new ShareAction(activity);
        init(context, share);
    }

    private void init(final Context context, final ShareInfo share) {
        this.context = context;
        getWindow().setGravity(Gravity.BOTTOM);//设置dialog显示的位置
        setContentView(R.layout.dialog_share);
        getWindow().setLayout(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);

        if (Build.VERSION.SDK_INT >= 23) {
            String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS};
            ActivityCompat.requestPermissions(((SettingActivity) context), mPermissionList, 123);
        }

        findViewById(R.id.tvShareCancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        if (share != null) {
            image = new UMImage(context, R.mipmap.ic_launcher); //资源文件
            thumb = new UMImage(context, R.mipmap.ic_launcher);  //微信QQ的那个平台，分享的图片需要设置缩略图
//			UMImage image = new UMImage(context, "imageurl");//网络图片
//			UMImage image = new UMImage(context, file);//本地文件
//			UMImage image = new UMImage(context, R.drawable.xxx);//资源文件
//			UMImage image = new UMImage(context, bitmap);//bitmap文件
//			UMImage image = new UMImage(context, byte[]);//字节流
            if (!StringUtils.isBlank(share.share_image.source)) {
                image = new UMImage(context, share.share_image.source);//网络图片
                thumb = new UMImage(context, share.share_image.thumb);//网络图片
                image.setThumb(thumb);
            }

            shareAction.withText(share.content);
            shareAction.setCallback(umShareListener);
            shareAction.withMedia(image);

            findViewById(R.id.llShareWeChat).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    UMWeb web = new UMWeb(share.share_url);
                    web.setTitle(share.title);//标题
                    web.setThumb(thumb);  //缩略图
                    web.setDescription(share.content);//描述
                    shareAction.withMedia(web);
                    shareAction.setPlatform(SHARE_MEDIA.WEIXIN);
                    shareAction.share();
                    dismiss();
                }
            });
            findViewById(R.id.llShareWeChatMoment).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    UMWeb web = new UMWeb(share.share_url);
                    web.setTitle(share.title);//标题
                    web.setThumb(thumb);  //缩略图
                    web.setDescription(share.content);//描述
                    shareAction.withMedia(web);
                    shareAction.setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE);
                    shareAction.share();
                    dismiss();
                }
            });
            //todo 注意新浪分享的title是不显示的，URL链接只能加在分享文字后显示，并且需要确保withText()不为空
            findViewById(R.id.llShareSina).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    shareAction.setPlatform(SHARE_MEDIA.SINA);
                    shareAction.share();
                    dismiss();
                }
            });
            findViewById(R.id.llShareQQ).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    shareAction.setPlatform(SHARE_MEDIA.QQ);
                    shareAction.share();
                    dismiss();
                }
            });
        }
    }

    /**
     * 回调监听
     */
    UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            if (platform.equals(SHARE_MEDIA.WEIXIN)) {
                toast("微信分享成功");
            }
            if (platform.equals(SHARE_MEDIA.WEIXIN_CIRCLE)) {
                toast("微信朋友圈分享成功");
            }
            if (platform.equals(SHARE_MEDIA.SINA)) {
                toast("新浪微博分享成功");
            }
            if (platform.equals(SHARE_MEDIA.QQ)) {
                toast("QQ分享成功");
            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            if (platform.equals(SHARE_MEDIA.WEIXIN)) {
                toast("微信分享失败");
            }
            if (platform.equals(SHARE_MEDIA.WEIXIN_CIRCLE)) {
                toast("微信朋友圈分享失败");
            }
            if (platform.equals(SHARE_MEDIA.SINA)) {
                toast("新浪微博分享失败");
            }
            if (platform.equals(SHARE_MEDIA.QQ)) {
                toast("QQ分享失败");
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            if (platform.equals(SHARE_MEDIA.WEIXIN)) {
                toast("微信分享已取消了");
            }
            if (platform.equals(SHARE_MEDIA.WEIXIN_CIRCLE)) {
                toast("微信朋友圈分享已取消");
            }
            if (platform.equals(SHARE_MEDIA.SINA)) {
                toast("新浪微博分享已取消");
            }
            if (platform.equals(SHARE_MEDIA.QQ)) {
                toast("QQ分享已取消");
            }
        }
    };

    private void toast(String text) {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
    }
}
